<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            height: 1500px;
          
        }
        p{
            width: 60px;
            height: 60px;
            background: lightslategray;
            color: #fff;
            font-size: 20px;
            line-height: 60px;
            text-align: center;
            position: fixed;
            right: 0;
            bottom: 0;
            cursor: pointer;
            user-select: none;
            display: none;
        }
    </style>
</head>
<body>
    <div>
        <p>TOP</p>
    </div>
</body>
</html>
<script src='https://code.jquery.com/jquery-3.6.0.min.js'></script>
<script>
    let p=$('p');
    if($(window).scrollTop()>=300){
        p.css('display','block')
    }
   $(window).scroll(function(){
    console.log($(window).scrollTop())
    if($(window).scrollTop()>=300){
        p.fadeIn(500)
    }else{
        p.fadeOut(500)
    }
   })
    p.click(function(){
      $('body,html').animate({scrollTop:0},1000);
    })
</script>